// KChildVersionControlWithVersion.qml
import QtQuick
import QtQuick.Controls
import Tool 1.0

Item {
    function mapTypeName(name){
        switch(name){
        case "MainExplanation":{
            return "主要说明"
        }
        case "VersionControl":{
            return "版本控制集"
        }
        case "Version":{
            return "版本"
        }
        case "mount":{
            return "挂载"
        }
        case "MountCollection":{
            return "挂载集合"

        }


        }
    }

    width: parent.width
    height: loader.implicitHeight

    property ListModel itemVersionControlOfKChildVersionControlWithVersion
    property int baseIndent: 20
    property int currentLevel: 1
    property var parentVersion: null  // 新增：父级版本信息

    Loader {
        id: loader
        width: parent.width
        sourceComponent: kchildversioncontrolwithversion_item
        onItemChanged: {
            if (item) {
                // item.parentVersion = parentVersion  // 传递父级版本信息
                item.currentLevel = currentLevel;
                item.baseIndent = baseIndent;
                item.versionControlOfKChildVersionControlWithVersion = itemVersionControlOfKChildVersionControlWithVersion;
            }
        }
    }

    Component {
        id: kchildversioncontrolwithversion_item

        ListView {
            id: listview_ofkcvcwv
            property ListModel versionControlOfKChildVersionControlWithVersion
            property int currentLevel: 0
            property int baseIndent: 20
            // property var parentVersion: null  // 接收父级版本信息

            implicitWidth: parent.width
            implicitHeight: 500
            height: contentItem.childrenRect.height
            // implicitHeight: contentItem.childrenRect.height
            interactive: false
            model: versionControlOfKChildVersionControlWithVersion

            // 统一排序函数（放在 ListView 根作用域）
            function sortModel() {
                if (!versionControlOfKChildVersionControlWithVersion) return;

                // 冒泡排序优化版
                for (var i = 0; i < model.count; i++) {
                    for (var j = 0; j < model.count - i - 1; j++) {
                        var current = model.get(j);
                        var next = model.get(j + 1);

                        // 关键：与父级版本比较
                        // if (parentVersion) {
                        //     if (current.MainVersion < parentVersion.MainVersion) {
                        //         model.move(j, j + 1, 1);
                        //     }
                        // }

                        // 同级排序
                        if (current.MainVersion > next.MainVersion) {
                            model.move(j, j + 1, 1);
                        } else if (current.MainVersion === next.MainVersion && current.MinorVersion > next.MinorVersion) {
                            model.move(j, j + 1, 1);
                        }
                    }
                }
            }

            // 初始化时排序
            Component.onCompleted: sortModel()

            delegate: Column {
                width: listview_ofkcvcwv.width

                // 主条目
                Rectangle {
                    width: parent.width
                    height: 30
                    color: "transparent" /*model.type==="Version"?"grey":*/
                    Component.onCompleted: {
                        // 委托加载时更新排序（可选）
                        listview_ofkcvcwv.sortModel()
                    }
                    // color:
                    Text {
                        visible: model.type==="VersionControl"
                        anchors {
                            left: parent.left
                            verticalCenter: parent.verticalCenter
                            leftMargin: currentLevel * baseIndent
                        }
                        text: `${model.name}--${mapTypeName(model.type)}-${model.MainVersion}.${model.MinorVersion}`
                    }
                    Text {
                        visible: model.type==="Version"
                        anchors {
                            left: parent.left
                            verticalCenter: parent.verticalCenter
                            leftMargin: currentLevel * baseIndent
                        }
                        text:model.name+"--"+mapTypeName(model.type)+"-"+model.MainVersion+"."+model.MinorVersion+"."+model.RevisionVersion
                        // text: `${model.name}--${mapTypeName(model.type)}-${model.MainVersion}.${model.MinorVersion}.${model.RevisionVersion}`
                    }

                    Text {
                        visible: model.type==="MainExplanation"
                        anchors {
                            left: parent.left
                            verticalCenter: parent.verticalCenter
                            leftMargin: currentLevel * baseIndent
                        }
                        text: `${model.name}--${mapTypeName(model.type)}`
                    }
                    KOperationButton {
                        // Component.onCompleted: {
                        //     kmodel=model

                        // }
                        kmodelindex: index
                        kmodel: model
                        klistmodel: versionControlOfKChildVersionControlWithVersion
                    }
                }
                // 版本列表
                // ListView {
                //     width: parent.width
                //     implicitHeight: contentItem.childrenRect.height
                //     model: MainExplanation
                //     interactive: false

                //     delegate: Rectangle {
                //         width: parent.width
                //         height: 30
                //         color: "#f5f5f5"

                //         Text {
                //             anchors {
                //                 left: parent.left
                //                 verticalCenter: parent.verticalCenter
                //                 leftMargin: (currentLevel + 1) * baseIndent // 子项缩进比主条目多一级
                //             }
                //             text: model.name + "--" + model.type + "-" +
                //                   model.MainVersion + "." +
                //                   model.MinorVersion + "." +
                //                   model.RevisionVersion
                //         }
                //         KOperationButton {
                //             kmodel: model
                //             klistmodel: MainExplanation
                //             kmodelindex: index
                //         }
                //     }
                // }
                // 递归加载子项
                Loader {
                    width: parent.width
                    sourceComponent: kchildversioncontrolwithversion_item
                    onLoaded: {
                        if (item && model.VersionControl) {
                            // 传递关键属性
                            // item.parentVersion = model  // 传递当前项作为子级的父版本
                            item.currentLevel = currentLevel + 1;
                            item.baseIndent = baseIndent;
                            item.versionControlOfKChildVersionControlWithVersion = model.VersionControl;

                            // 子级初始化后排序
                            item.sortModel();
                        }
                    }
                }
            }
        }
    }
}
